package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.dto.LikesBehaviorDto;
import com.heima.behavior.entity.ApLikesBehavior;
import com.heima.behavior.entity.UpdateArticleMessage;
import com.heima.behavior.service.ILikeService;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.util.AppThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;


import java.util.Date;

@Service
public class LikeServiceImpl implements ILikeService {
    @Autowired
    private KafkaTemplate kafkaTemplate;
    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public ResponseResult saveLike(LikesBehaviorDto dto) {
        //构建对象
        ApLikesBehavior behavior=new ApLikesBehavior();
        User user = AppThreadLocalUtil.get();
        if(user==null){
            behavior.setEquipmentId(dto.getEquipmentId());
        }else{
            behavior.setUserId(user.getUserId());
        }
        behavior.setArticleId(dto.getArticleId());
        behavior.setType(0);
        behavior.setOperation(dto.getOperation());
        behavior.setCreatedTime(new Date());
        //判断有点赞记录
        Query query=new Query();
        query.addCriteria(Criteria.where("articleId").is(dto.getArticleId()));
        if(user==null){
            query.addCriteria(Criteria.where("equipmentId").is(dto.getEquipmentId()));
        }else{
            query.equals(Criteria.where("userId").is(user.getUserId()));
        }
        ApLikesBehavior one = mongoTemplate.findOne(query, ApLikesBehavior.class);

        if(one==null){
            if(dto.getOperation()==0){
                mongoTemplate.save(behavior);
                sendMessage(dto);
            }
        }else{
            if(dto.getOperation()!=one.getOperation()){
                one.setOperation(dto.getOperation());
                mongoTemplate.save(one);
                sendMessage(dto);
            }
        }
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getLikes(LikesBehaviorDto dto) {
        Query query=new Query();
        query.addCriteria(Criteria.where("articleId").is(dto.getArticleId()));
        if(dto.getUserId()==null){
            query.addCriteria(Criteria.where("equipmentId").is(dto.getEquipmentId()));
        }else{
            query.equals(Criteria.where("userId").is(dto.getUserId()));
        }
        ApLikesBehavior one = mongoTemplate.findOne(query, ApLikesBehavior.class);
        return ResponseResult.okResult(one);
    }

    private void sendMessage(LikesBehaviorDto dto) {
        UpdateArticleMessage message=new UpdateArticleMessage();
        message.setType(1);
        message.setArticleId(dto.getArticleId());
        message.setAdd(1);
        if(dto.getOperation()==1){
            message.setAdd(-1);
        }
        String value = JSON.toJSONString(message);
        kafkaTemplate.send(LikeTopic,value);
    }

    @Value("${topic.like_behavior}")
    private String LikeTopic;

}
